﻿/*
* BDValueFunc.cs
*
* 功 能： 与特征值表交互的通用类
* 类 名： BDValueFunc
*
* Ver    变更日期  20131125   负责人  黄永生  变更内容
* ───────────────────────────────────

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.Model;
using DQYT.HTDZ.BLL;
using System.Data;
using DQYT.HTDZ.BusinessService;
namespace DQYT.HTDZ.BusinessService
{
    /// <summary>
    /// 将BbDatasourceModel（采集数据表）中的数据计算后存储到BDVALUE（特征值表）中
    /// </summary>
    public class BDValueFunc
    {
        private BDDATESOURSEBLL datasoursebll = new BDDATESOURSEBLL();
        private BDVALUEBLL valuebll = new BDVALUEBLL();
        private BDVALUEModel valueModel = new BDVALUEModel();
        private DataTable candt;//存放单个参数数据的表
        private BDCjsjFunc cjsjFunc = new BDCjsjFunc();

       
        /// <summary>
        /// 获得特征值model
        /// </summary>
        /// <param name="wellid"></param>
        /// <param name="jcrq"></param>
        /// <returns></returns>
        public BDVALUEModel getValueModel(string wellid, string jcrq)
        {
            DataSet ds = new DataSet();
            string sqlstr = "select * from BDVALUE where JCRQ=to_date( '" + jcrq + "','yyyy-MM-dd HH24:mi:ss ') and trim(WELLID)='" + wellid + "'";
            BDVALUEBLL bll = new BDVALUEBLL();
            ds = bll.GetList_SQL(sqlstr);
            decimal id = (decimal)ds.Tables[0].Rows[0][0];
            BDVALUEModel model = new BDVALUEModel();
            model = bll.GetModel(id);
            return model;


        }
        /// <summary>
        /// 设置特征值对象
        /// </summary>
        /// <param name="datasouseModel"></param>
        public void setValue(BDDATESOURCEModel datasouseModel)
        {
            //valueModel.ID = datasouseModel.ID;
            valueModel.WELLID = datasouseModel.WELLID;
            valueModel.JCRQ = datasouseModel.JCRQ;
            valueModel.KSSJ = datasouseModel.KSSJ;
            valueModel.JSSJ = datasouseModel.JSSJ;
            List<CJSJModel> cjsjModels = BinarySerializer.DeserializeObject<List<CJSJModel>>(datasouseModel.CJSJ);
            //有功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HYGGL");
            getHYGGL(candt);
            //无功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HWGGL");
            getHWGGL(candt);
            //视在功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HSZGL");
            getHSZGL(candt);
            //功率因数特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HGLYS");
            getHGLYS(candt);
            //电流特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADL");
            getADL(candt);
            //电压特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADY");
            getADY(candt);
            ////扭矩特征值
            //candt = listCJSJModelToDataTable(cjsjModels, "DJSCNJ");
            //getDJSCNJ(candt);
            //初始化model数据
            valuebll.Add(valueModel);

        }
        /// <summary>
        /// 设置特征值对象；临时导华北油田数据用的
        /// </summary>
        /// <param name="datasouseModel"></param>
        public void setValue_TEMP(BDDATESOURCEModel datasouseModel,float cc,float cs)
        {
            //valueModel.ID = datasouseModel.ID;
            valueModel.WELLID = datasouseModel.WELLID;
            valueModel.JCRQ = datasouseModel.JCRQ;
            valueModel.KSSJ = datasouseModel.KSSJ;
            valueModel.JSSJ = datasouseModel.JSSJ;
            valueModel.CYJCC =(decimal)cs;
            valueModel.CYJCCI = (decimal)cc;
            valueModel.JSSJ = datasouseModel.JSSJ;
            List<CJSJModel> cjsjModels = BinarySerializer.DeserializeObject<List<CJSJModel>>(datasouseModel.CJSJ);
            //有功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HYGGL");
            getHYGGL(candt);
            //无功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HWGGL");
            getHWGGL(candt);
            //视在功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HSZGL");
            getHSZGL(candt);
            //功率因数特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HGLYS");
            getHGLYS(candt);
            //电流特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADL");
            getADL(candt);
            //电压特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADY");
            getADY(candt);
            ////扭矩特征值
            //candt = listCJSJModelToDataTable(cjsjModels, "DJSCNJ");
            //getDJSCNJ(candt);
            //初始化model数据
            valuebll.Add(valueModel);

        }
        /// <summary>
        /// 通过基础数据提取特征值
        /// </summary>
        /// <param name="datasouseModel"></param>
        /// <returns></returns>
        public BDVALUEModel getValue(BDDATESOURCEModel datasouseModel)
        {
            List<CJSJModel> cjsjModels = BinarySerializer.DeserializeObject<List<CJSJModel>>(datasouseModel.CJSJ);
            //有功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HYGGL");
            getHYGGL(candt);
            //无功功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HWGGL");
            getHWGGL(candt);
            //视在功率特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HSZGL");
            getHSZGL(candt);
            //功率因数特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "HGLYS");
            getHGLYS(candt);
            //电流特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADL");
            getADL(candt);
            //电压特征值
            candt = cjsjFunc.listCJSJModelToDataTable(cjsjModels, "ADY");
            getADY(candt);
            return valueModel;
        }
        /// <summary>
        /// 有功功率特征值计算方法
        /// </summary>
        public void getHYGGL(DataTable data)
        {
            DataTable updata = null;//上行数据
            DataTable downdata = null;//下行数据
            double upmax;//上行有功最大值
            double downmax;//下行有功最大值
            double maxbalance; //有功最大值平衡度
            double upaveg; //上行有功平均值
            double downaveg; //下行有功平均值
            double avegbalance; //有功平均值平衡度
            double nagerata; //有功负值率
            double pzrata; //有功偏置率
            double posivalue = 0; //日有功正能耗
            double nagevalue = 0; //日有功负能耗

            //提取上行和下行数据
            // 复制表结构
            updata = data.Clone();
            downdata = data.Clone();
            int j = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {

                if (i < data.Rows.Count / 2)
                {
                    // 添加空行
                    DataRow row = updata.NewRow();
                    updata.Rows.Add(row);
                    updata.Rows[i][0] = data.Rows[i][0];
                }
                else
                {
                    // 添加空行
                    DataRow row = downdata.NewRow();
                    downdata.Rows.Add(row);
                    downdata.Rows[j++][0] = data.Rows[i][0];
                }
            }
            //最大值
            upmax = getValueMax(updata);
            downmax = getValueMax(downdata);
            maxbalance = downmax / upmax;
            valueModel.SXYGMAX = (decimal)upmax;
            valueModel.XXYGMAX = (decimal)downmax;
            valueModel.YGZDZPHD = (decimal)maxbalance;
            //平均值
            upaveg = getPosiValueAveg(updata);
            downaveg = getPosiValueAveg(downdata);
            avegbalance = downaveg / upaveg;
            valueModel.SXYGPJZ = (decimal)upaveg;
            valueModel.XXYGPJZ = (decimal)downaveg;
            valueModel.YGPJZPHD = (decimal)avegbalance;
            //负值率、日有功正能耗、日有功负能耗
            for (int i = 0; i < data.Rows.Count; i++)
            {
                if ((double)data.Rows[i][0] > 0)
                {
                    posivalue += (double)data.Rows[i][0];
                }
                else
                {
                    nagevalue += (double)data.Rows[i][0];
                }

            }
            posivalue = (posivalue / 256) * 24;
            nagevalue = Math.Abs((nagevalue / 256) * 24);
            nagerata = nagevalue / (nagevalue + posivalue);
            valueModel.RYGZNH = (decimal)posivalue;
            valueModel.RYGFNH = (decimal)nagevalue;
            valueModel.YGFZL = (decimal)nagerata;
            //偏置率
            double upmin = (double)updata.Rows[0][0];
            double downmin = (double)downdata.Rows[0][0];
            for (int i = 1; i < updata.Rows.Count; i++)
            {
                if ((double)updata.Rows[i][0] < upmin) upmin = (double)updata.Rows[i][0];
            }
            for (var i = 1; i < downdata.Rows.Count; i++)
            {
                if ((double)downdata.Rows[i][0] < downmin) downmin = (double)downdata.Rows[i][0];
            }
            pzrata = downmin / upmin;
            valueModel.YGPZL = (decimal)pzrata;
        }
        /// <summary>
        /// 无功功率特征值计算方法
        /// </summary>
        public void getHWGGL(DataTable data)
        {
            double aveg;//平均无功功率
            double rwgnh;//日无功能耗
            aveg = getValueAveg(data);
            rwgnh = (double)data.Compute("Sum(HWGGL)", "true");
            valueModel.PJWGGL = (decimal)aveg;
            valueModel.RWGNH = (decimal)rwgnh;

        }
        /// <summary>
        /// 视在功率特征值计算方法
        /// </summary>
        public void getHSZGL(DataTable data)
        {
            double aveg;//平均视在功率
            aveg = getValueAveg(data);
            valueModel.PJSZGL = (decimal)aveg;

        }
        /// <summary>
        /// 功率因数特征值计算方法
        /// </summary>
        public void getHGLYS(DataTable data)
        {
            double aveg;//平均功率因数
            aveg = getValueAveg(data);
            valueModel.PJGLYS = (decimal)aveg;

        }
        /// <summary>
        /// 电流特征值计算方法
        /// </summary>
        public void getADL(DataTable data)
        {
            DataTable updata = null;//上行数据
            DataTable downdata = null;//下行数据
            double upmax;//上行最大值
            double downmax;//下行最大值
            double maxbalance; //最大值平衡度
            //提取上行和下行数据
            // 复制表结构
            updata = data.Clone();
            downdata = data.Clone();
            int j = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {

                if (i < data.Rows.Count / 2)
                {
                    // 添加空行
                    DataRow row = updata.NewRow();
                    updata.Rows.Add(row);
                    updata.Rows[i][0] = data.Rows[i][0];
                }
                else
                {
                    // 添加空行
                    DataRow row = downdata.NewRow();
                    downdata.Rows.Add(row);
                    downdata.Rows[j++][0] = data.Rows[i][0];
                }
            }
            //最大值
           
            upmax = getValueMax(updata);
            downmax = getValueMax(downdata);
            if (upmax == 0)
            {
                maxbalance = 0;
            }
            else
            {
                maxbalance = downmax / upmax;
            }
            valueModel.SXDLMAX = (decimal)upmax;
            valueModel.XXDLMAX = (decimal)downmax;
            valueModel.DDZDZPHD = (decimal)maxbalance;

        }
        /// <summary>
        /// 电压特征值计算方法
        /// </summary>
        public void getADY(DataTable data)
        {
            double aveg;//平均电压
            aveg = getValueAveg(data);
            valueModel.PJXDY = (decimal)aveg;

        }
        /// <summary>
        /// 扭矩特征值计算方法
        /// </summary>
        /// <param name="data"></param>
        public void getDJSCNJ(DataTable data)
        {
            DataTable updata = null;//上行数据
            DataTable downdata = null;//下行数据
            double upsqrt = 0;
            double downsqrt = 0;
            double balance = 0; //平衡度
            double nagerata = 0; //负值率
            double pzrata = 0; //偏置率
            //提取上行和下行数据
            // 复制表结构
            updata = data.Clone();
            downdata = data.Clone();
            int j = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {

                if (i < data.Rows.Count / 2)
                {
                    // 添加空行
                    DataRow row = updata.NewRow();
                    updata.Rows.Add(row);
                    updata.Rows[i][0] = data.Rows[i][0];
                }
                else
                {
                    // 添加空行
                    DataRow row = downdata.NewRow();
                    downdata.Rows.Add(row);
                    downdata.Rows[j++][0] = data.Rows[i][0];
                }
            }
            //上行均方根
            for (int i = 0; i < updata.Rows.Count; i++)
            {
                upsqrt += ((double)updata.Rows[i][0]) * ((double)updata.Rows[i][0]);
            }
            upsqrt = Math.Sqrt(upsqrt / updata.Rows.Count);
            valueModel.SXNJJFG = (decimal)upsqrt;
            //下行均方根
            for (int i = 0; i < downdata.Rows.Count; i++)
            {
                downsqrt += ((double)downdata.Rows[i][0]) * ((double)downdata.Rows[i][0]);
            }
            downsqrt = Math.Sqrt(downsqrt / downdata.Rows.Count);
            valueModel.XXNJJFG = (decimal)downsqrt;
            //平衡度
            if (upsqrt != 0)
                balance = downsqrt / upsqrt;
            else
                balance = 1;
            //偏置率
            double max = getValueMax(updata);
            double min = (double)updata.Rows[0][0];
            for (int i = 1; i < updata.Rows.Count; i++)
            {
                if ((double)updata.Rows[i][0] <= min) min = (double)updata.Rows[i][0];
            }
            if (max - min != 0)
            {
                pzrata = ((double)updata.Rows[updata.Rows.Count - 1][0] - (double)updata.Rows[0][0]) / (max - min);
            }
            else
            {
                pzrata = 1;
            }
            valueModel.NJPZL = (decimal)pzrata;
            //负值率
            double posimult = 0;
            double nagemult = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                if ((double)data.Rows[i][0] > 0)
                {
                    posimult += (double)data.Rows[i][0];
                }
                else
                {
                    nagemult += (double)data.Rows[i][0];
                }
            }
            nagerata = Math.Abs(nagemult) / (posimult + Math.Abs(nagemult));
            valueModel.NJFZL = (decimal)nagerata;

        }

        /// <summary>
        /// 获得一组数中的最大值
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public double getValueMax(DataTable data)
        {
            double max = (double)data.Rows[0][0];
            for (int i = 1; i < data.Rows.Count; i++)
            {
                if ((double)data.Rows[i][0] >= max) max = (double)data.Rows[i][0];
            }
            return max;
        }
        /// <summary>
        /// 求一组数据的平均值
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public double getValueAveg(DataTable data)
        {
            int i;
            double aveg = 0;
            for (i = 0; i < data.Rows.Count; i++)
            {
                aveg += (double)data.Rows[i][0];
            }
            if (i == 0) return 0;
            aveg = aveg / i;
            return aveg;
        }
        /// <summary>
        /// 求一组数据的所有正数平均值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public double getPosiValueAveg(DataTable data)
        {
            int i;
            double aveg = 0;
            for (i = 0; i < data.Rows.Count; i++)
            {
                if ((double)data.Rows[i][0] > 0)
                {
                    aveg += (double)data.Rows[i][0];
                }
            }
            aveg = aveg / i;
            return aveg;
        }
        /// <summary>
        /// 求一组数据的所有负数平均值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public double getNageValueAveg(DataTable data)
        {
            int i;
            double aveg = 0;
            for (i = 0; i < data.Rows.Count; i++)
            {
                if ((double)data.Rows[i][0] < 0)
                {
                    aveg += (double)data.Rows[i][0];
                }
            }
            aveg = aveg / i;
            return aveg;
        }


    }

}
